//
//  UICollectionView.swift
//  Documentation
//
//  Created by Toj on 11/18/22.
//
//  UICollectionView
//

open class UICollectionView : UIScrollView, UIDataSourceTranslating {
    
    public init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout)
    
    // 布局代理对象
    open var collectionViewLayout: UICollectionViewLayout
    
    // 交互代理
    weak open var delegate: UICollectionViewDelegate?
    
    // 数据源代理
    weak open var dataSource: UICollectionViewDataSource?
    
    
    @available(iOS 10.0, *)
    // 预加载代理
    weak open var prefetchDataSource: UICollectionViewDataSourcePrefetching?
    
    @available(iOS 10.0, *)
    // 属性默认开启, 会将点击的cell放置在最上方
    open var isPrefetchingEnabled: Bool
    
    
    @available(iOS 11.0, *)
    weak open var dragDelegate: UICollectionViewDragDelegate?
    
    @available(iOS 11.0, *)
    weak open var dropDelegate: UICollectionViewDropDelegate?
    
    
    /*
     * You can force drags to be disabled for this collection view by setting this to NO.
     * As of iOS 15, this is true for both iPhone and iPad by default. Prior to iOS 15, it defaulted to false on iPhone.
     */
    @available(iOS 11.0, *)
    open var dragInteractionEnabled: Bool
    
    
    @available(iOS 13.2, *)
    open var contextMenuInteraction: UIContextMenuInteraction? { get }
    
    
    /* Reordering cadence affects how easily reordering occurs while dragging around a reorder-capable drop destination.
     * Default is UICollectionViewReorderingCadenceImmediate.
     */
    @available(iOS 11.0, *)
    open var reorderingCadence: UICollectionView.ReorderingCadence
    
    // 背景视图将自动调整大小, 以跟踪表视图的大小.
    // 并放置在所有单元格和补充视图的后面
    open var backgroundView: UIView?
    
    
    
    // 注册AnyClass单元格
    open func register(_ cellClass: AnyClass?, forCellWithReuseIdentifier identifier: String)
    
    // 注册UINib单元格
    open func register(_ nib: UINib?, forCellWithReuseIdentifier identifier: String)
    
    
    // 注册AnyClass Header/ Footer
    open func register(_ viewClass: AnyClass?, forSupplementaryViewOfKind elementKind: String, withReuseIdentifier identifier: String)
    
    // 注册UINib Header/ Footer
    open func register(_ nib: UINib?, forSupplementaryViewOfKind kind: String, withReuseIdentifier identifier: String)
    
    
    // 获取重用队列里的单元格
    // 委托用来获取一个已经分配的单元格, 而不是分配一个新的单元格
    open func dequeueReusableCell(withReuseIdentifier identifier: String, for indexPath: IndexPath) -> UICollectionViewCell
    
    // 获取重用队列里的单元格
    // 更新的出队列方法保证返回一个单元格并正确调整其大小, 假定标识符已注册
    open func dequeueReusableSupplementaryView(ofKind elementKind: String, withReuseIdentifier identifier: String, for indexPath: IndexPath) -> UICollectionReusableView
    
    
    // 默认为YES. 在非编辑下, 行是否可以选中
    open var allowsSelection: Bool
    
    // 默认为NO. 是否可以同时选择多个行
    open var allowsMultipleSelection: Bool
    
    // 返回选择的所有的单元格的索引
    open var indexPathsForSelectedItems: [IndexPath]? { get } // returns nil or an array of selected index paths
    
    // 选中某个索引的单元格
    open func selectItem(at indexPath: IndexPath?, animated: Bool, scrollPosition: UICollectionView.ScrollPosition)
    // 取消选中的单元格
    open func deselectItem(at indexPath: IndexPath, animated: Bool)
    
    
    // Returns YES if the collection view is reordering or has drop placeholders.
    @available(iOS 11.0, *)
    open var hasUncommittedUpdates: Bool { get }
    
    
    // 注意:-reloadData将丢弃任何未提交的更新(例如占位符)
    // 丢弃数据源, 并根据需要委派数据和查询
    open func reloadData()
    
    
    open func setCollectionViewLayout(_ layout: UICollectionViewLayout, animated: Bool) // transition from one layout to another
    
    @available(iOS 7.0, *)
    open func setCollectionViewLayout(_ layout: UICollectionViewLayout, animated: Bool, completion: ((Bool) -> Void)? = nil)
    
    
    @available(iOS 7.0, *)
    open func startInteractiveTransition(to layout: UICollectionViewLayout, completion: UICollectionView.LayoutInteractiveTransitionCompletion? = nil) -> UICollectionViewTransitionLayout
    
    @available(iOS 7.0, *)
    open func finishInteractiveTransition()
    
    @available(iOS 7.0, *)
    open func cancelInteractiveTransition()
    
    
    // Information about the current state of the collection view.
    // 获取当前所有的组
    open var numberOfSections: Int { get }
    // 获取某个组有多少数量
    open func numberOfItems(inSection section: Int) -> Int
    
    
    open func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
    
    open func layoutAttributesForSupplementaryElement(ofKind kind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
    
    
    // 点击某一个点, 判断是在哪一行上的信息.
    open func indexPathForItem(at point: CGPoint) -> IndexPath?
    // 获取单元格的信息
    open func indexPath(for cell: UICollectionViewCell) -> IndexPath?
    
    
    // 通过单元格路径得到单元格
    // 当没有单元格存在或索引路径超出范围时返回nil
    open func cellForItem(at indexPath: IndexPath) -> UICollectionViewCell?
    
    // 返回所有可见的单元格
    open var visibleCells: [UICollectionViewCell] { get }
    
    // 返回所有可见行的单元格索引
    open var indexPathsForVisibleItems: [IndexPath] { get }
    
    
    @available(iOS 9.0, *)
    // 通过单元格索引得到 UICollectionReusableView
    open func supplementaryView(forElementKind elementKind: String, at indexPath: IndexPath) -> UICollectionReusableView?
    
    @available(iOS 9.0, *)
    // 返回所有可见的 UICollectionReusableView
    open func visibleSupplementaryViews(ofKind elementKind: String) -> [UICollectionReusableView]
    
    @available(iOS 9.0, *)
    // 返回所有可见行的UICollectionReusableView索引
    open func indexPathsForVisibleSupplementaryElements(ofKind elementKind: String) -> [IndexPath]
    
    
    // 滑动到指定索引位置
    open func scrollToItem(at indexPath: IndexPath, at scrollPosition: UICollectionView.ScrollPosition, animated: Bool)
    
    
    // 插入一个或多个组
    open func insertSections(_ sections: IndexSet)
    
    // 删除一个或多个组
    open func deleteSections(_ sections: IndexSet)
    
    // 移动某个组到目标组位置
    open func moveSection(_ section: Int, toSection newSection: Int)
    
    // 更新一个或多个组
    open func reloadSections(_ sections: IndexSet)
    
    // 插入一个或多个单元格
    open func insertItems(at indexPaths: [IndexPath])
    
    // 删除一个或多个单元格
    open func deleteItems(at indexPaths: [IndexPath])
    
    // 移动某个单元格到目标单元格位置
    open func moveItem(at indexPath: IndexPath, to newIndexPath: IndexPath)
    // 更新一个或多个单元格
    open func reloadItems(at indexPaths: [IndexPath])
    
    // Reconfigures any existing cells for the items. Reconfiguring is more efficient than reloading an item, as it does not replace the
    // existing cell with a new cell. Prefer reconfiguring over reloading unless you actually need an entirely new cell for the item.
    @available(iOS 15.0, *)
    open func reconfigureItems(at indexPaths: [IndexPath])
    
    
    open func performBatchUpdates(_ updates: (() -> Void)?, completion: ((Bool) -> Void)? = nil) // allows multiple insert/delete/reload/move calls to be animated simultaneously. Nestable.
    
    
    // Support for reordering
    @available(iOS 9.0, *)
    open func beginInteractiveMovementForItem(at indexPath: IndexPath) -> Bool // returns NO if reordering was prevented from beginning - otherwise YES
    
    @available(iOS 9.0, *)
    open func updateInteractiveMovementTargetPosition(_ targetPosition: CGPoint)
    
    @available(iOS 9.0, *)
    open func endInteractiveMovement()
    
    @available(iOS 9.0, *)
    open func cancelInteractiveMovement()
    
    
    // Support for Focus
    @available(iOS 9.0, *)
    open var remembersLastFocusedIndexPath: Bool // defaults to NO. If YES, when focusing on a collection view the last focused index path is focused automatically. If the collection view has never been focused, then the preferred focused index path is used.
    
    
    /// When enabled, the collection view ensures that selection is automatically triggered when focus moves to a cell.
    /// Defaults to a system derived value based on platform and other properties of the collection view.
    @available(iOS 14.0, *)
    open var selectionFollowsFocus: Bool
    
    
    /// Determines if the collection view allows its cells to become focused.
    /// When collectionView:canFocusItemAtIndexPath: is implemented, its return value takes precedence over this method.
    /// Defaults to a system derived value based on platform and other properties of the collection view.
    @available(iOS 15.0, *)
    open var allowsFocus: Bool
    
    
    /// Determines if the collection view allows its cells to become focused while editing.
    /// When collectionView:canFocusItemAtIndexPath: is implemented, its return value takes precedence over this method.
    /// Defaults to a system derived value based on platform and other properties of the collection view.
    @available(iOS 15.0, *)
    open var allowsFocusDuringEditing: Bool
    
    
    // Drag & Drop
    
    /* YES if a drag session is currently active. A drag session begins after items are "lifted" from the collection view.
     */
    @available(iOS 11.0, *)
    open var hasActiveDrag: Bool { get }
    
    
    /* YES if collection view is currently tracking a drop session.
     */
    @available(iOS 11.0, *)
    open var hasActiveDrop: Bool { get }
    
    
    // Editing
    
    // 编辑. 设置后, 行显示基于数据源查询的插入/删除/重新排序控件
    @available(iOS 14.0, *)
    open var isEditing: Bool
    
    
    
    // 默认为NO. 控制某一行时, 是否可以编辑
    @available(iOS 14.0, *)
    open var allowsSelectionDuringEditing: Bool
    
    
    //默认为NO. 在选择多行的情况下, 是否可以编辑
    @available(iOS 14.0, *)
    open var allowsMultipleSelectionDuringEditing: Bool
}
